
/*
 * Author Ahmed Abdelhalim - 2009
 * Email: englemo@hotmail.com
 * Please do not remove the above lines
 */

package remoteclient;

import Command.CommandStatement;
import Command.Constant_Command;
import clientprogram.SocketCommunication;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;

/**
 *
 * This class is responsible for connecting to the server
 * and starting ScreenSpyer and ServerDelegate classes
 */
public class ClientInitiator extends Thread {

    Socket socket = null;
    Robot robot = null;
    Rectangle rectangle = null;
    public boolean IsWorking = false;
    public ClientInitiator(){
        try {
            //Get default screen device
            GraphicsEnvironment gEnv=GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gDev=gEnv.getDefaultScreenDevice();

            //Get screen dimensions
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            rectangle = new Rectangle(dim);

            //Prepare Robot object
            robot = new Robot(gDev);
            start();
        } catch (AWTException ex) {
                ex.printStackTrace();
        }
    }
    
     public void run(){
         IsWorking = true;
        while(IsWorking){
             BufferedImage image = robot.createScreenCapture(rectangle);
             ImageIcon imageIcon = new ImageIcon(image);
             System.out.println("before sending image");
             SocketCommunication.Instance().SendCommand(new CommandStatement(Constant_Command.TEAMVIEW, imageIcon));
             System.out.println("New screenshot sent");
             //wait for 100ms to reduce network traffic
             try{
                 Thread.sleep(100);
             }catch(InterruptedException e){
                 e.printStackTrace();
             }
         }
        IsWorking = false;
    }
}
